package reflect;

import pojo.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @projectName: java-sample
 * @description:
 * @author: Adam
 * @create: 2019-10-17 15:47
 **/

public class RefelectDemo {

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        new RefelectDemo().testUserMethod("getName");
    }

    public void testUserMethod(String methodName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
       /* Person person = new Person();
        person.setAge(12);
        person.setName("张洪亮");*/
        Class clz = Person.class;
        Constructor constructor = clz.getConstructor();
        Person person1 = (Person) constructor.newInstance();
        Method setName = clz.getDeclaredMethod("setName", String.class);
        //person1为实例
        setName.invoke(person1,"张洪亮");
        Method targetMethon = clz.getDeclaredMethod(methodName);

        System.out.println(targetMethon.invoke(person1));
    }

}
